<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /login.php
# 分类列表页面

# 载入常用集
# 导入common
require_once (dirname(__FILE__).'/../include/common.inc.php');
# 页面级别
$conf_web_page_level = 0;

class page_index extends page{
	function __construct(){
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
	}
	
	# 密码登录操作
	function login($id,$passwd,$save){
		$sql = "SELECT loginId, adminId, cs2_admin.name, cs2_admin.level, cs2_level.levelno, cs2_admin.rootAdmin,
				cs2_level.name AS levelname,cs2_admin.checker,cs2_admin.`token` FROM cs2_admin LEFT JOIN cs2_level 
			ON (cs2_level.levelId = cs2_admin.level) 
			WHERE (loginId='".$id."' AND passwd='".md5($passwd)."' )";
		$row = $this->database->get_one($sql);
		if($row == null)
			header('Location:login.php?call=iperror');
		else{
			$_SESSION[config::conf_session_admin] = $row;
			# set cookie
			
			# 1周自动模式
			if($save == 'on'){
				setcookie('login', '', time() + 3600*24*7);
				setcookie('login[save]', 'on', time() + 3600*24*7);
				setcookie('login[token]', $row['token'], time() + 3600*24*7);
			# 不自动登录模式
			}else{
				setcookie('login','',time() - 3600);
				setcookie('login[save]', '',time() - 3600);
				setcookie('login[token]', '',time() - 3600);
			}
			header('Location:default.php');
		}
	}
	# 自动登录操作
	function login_auto($token,$uri){
		$sql = "SELECT loginId, adminId, cs2_admin.name, cs2_admin.level, cs2_level.levelno, cs2_admin.rootAdmin,
				cs2_level.name AS levelname,cs2_admin.checker,cs2_admin.`token` FROM cs2_admin LEFT JOIN cs2_level 
			ON (cs2_level.levelId = cs2_admin.level) 
			WHERE (cs2_admin.`token`='".$token."' )";
		
		$row = $this->database->get_one($sql);
		if($row == null)
			header('Location:login.php?call=iperror');
		else{
			$_SESSION[config::conf_session_admin] = $row;
			# 回调到指定页面
			header('Location:'.$uri);
		}
	}
	function ajax_callback(){
	}
}

if(isset($_SESSION[config::conf_session_admin])){
	header('Location:default.php');
}else if(isset($_COOKIE["login"])){
	header('Location:login.php?call=cookie&uri=default.php');
	#header('Location:login.php');
}

$pg = new page_index();
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$call = '';
	if(isset($_GET['call']))
		$call = $_GET['call'];
	
	# call 分析
	if($call == 'iperror')
		$config->smarty->assign('call', 'ログイン失敗しました。');
	
	# call 登出操作
	else if($call == 'logout'){
		$_SESSION[config::conf_session_admin] = null;
		$config->smarty->assign('call', '');
		setcookie('login','',time() - 3600);
		setcookie('login[save]', '',time() - 3600);
		setcookie('login[token]', '',time() - 3600);
	# call cookie 回调
	}else if($call == 'cookie'){
		if(isset($_COOKIE["login"])){
			$uri = $_GET['uri'];
			$pg->login_auto($_COOKIE["login"]['token'],$uri);
		}
	}else{
		$config->smarty->assign('call', '');
		
	}
	
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	# 加载页面
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	# check login info
	$save = '';
	if(isset($_POST['saveLoingId']))
		$save = $_POST['saveLoingId'];
	$pg->login($_POST['loingId'],$_POST['loingPass'],$save);
}


?>